package com.cet.algorithm.双指针法.移除元素;

import java.util.Arrays;

/**
 * @program: algorithm
 * @description: 有序数组的平方
 * @author: 陈恩涛
 * @create: 2022-03-23 21:27
 **/
public class LC977 {

    public int[] sortedSquares(int[] nums) {
        int left = 0;
        int right = nums.length - 1;
        int[] result = new int[nums.length];
        int index = nums.length - 1;
        while (left <= right) {
            int i = nums[left] * nums[left];
            int j = nums[right] * nums[right];
            if (i >= j) {
                result[index--] = i;
                left++;
            } else {
                result[index--] = j;
                right--;
            }
        }
        return result;
    }

    public static void main(String[] args) {
        int[] nums = {-7, -3, 2, 3, 11};
        int[] result = new LC977().sortedSquares(nums);
        Arrays.stream(result).forEach(System.out::println);
    }
}
